//
//  ScanViewController.m
//  QRBuildScan
//
//  Created by  on 12-1-2.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ScanViewController.h"
#import "InputContentController.h"
#import "ShowQRViewController.h"
#define InCellImgTag 0x10
#define InCellTextTag 0x20
#define NavBarTitleTag 0x21

@implementation ScanViewController

-(id)init {
    if ((self =[super init])) {
        self.delegate =self;
        p_CellRowTitleArr = [[NSArray alloc] initWithObjects:@"创建“网站网址”码",@"创建“电话号码”码",@"创建“文本”码",@"创建“地理位置”码",@"创建“电邮地址”码",@"创建“Mecard联系”码",@"创建“短信”码",nil];
        p_NavbarTitleArr = [[NSArray alloc] initWithObjects:@"网站网址",@"电话号码",@"文本",@"地理位置",@"电邮地址",@"Mecard联系",@"短信", nil];
        p_ScanRootController = [[UIViewController alloc] init];
        [self pushViewController:p_ScanRootController animated:NO];
        UIImageView* tCBarBg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navbg.png"]];
        tCBarBg.frame =CGRectMake(0, 0, ScreenWidth, 48);
        [self.navigationBar addSubview:tCBarBg];
        [tCBarBg release];
        
        UILabel* tNavBarTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 4, 120, 36)];
        tNavBarTitle.tag = NavBarTitleTag;
        tNavBarTitle.textAlignment =UITextAlignmentCenter;
        [tNavBarTitle setBackgroundColor:[UIColor clearColor]];
        [tNavBarTitle setFont:[UIFont systemFontOfSize:26]];
        [tNavBarTitle setTextColor:[UIColor whiteColor]];
        [self.navigationBar addSubview:tNavBarTitle];
        [tNavBarTitle release];
        
        UITableView* tTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        tTableView.delegate =self;
        tTableView.dataSource =self;
        tTableView.frame =CGRectMake(0, 0, ScreenWidth, ScreenHeight-44-50);
        [p_ScanRootController.view addSubview:tTableView];
        [tTableView release];
        
        p_CellImageTitleArr = [[NSMutableArray alloc] init];
        for (int i=0; i<[p_CellRowTitleArr count]; i++) {
            UIImage* tTempImg = [UIImage imageNamed:[NSString stringWithFormat:@"bqrtype%d.png",i]];
            [p_CellImageTitleArr addObject:tTempImg ];
        }
        
        
        p_PopNavButton = [UIButton buttonWithType:UIButtonTypeCustom];
        p_PopNavButton.frame = CGRectMake(6,7, 60, 30);
        [p_PopNavButton setBackgroundImage:[UIImage imageNamed:@"cancel.png"] forState:UIControlStateNormal];
        [p_PopNavButton addTarget:self action:@selector(PopNavgation) forControlEvents:UIControlEventTouchUpInside];
        [p_PopNavButton retain];
    }
    return self;
}

-(void) dealloc{
    [p_PopNavButton release];
    [p_NavbarTitleArr release];
    [p_CellImageTitleArr release];
    [p_CellRowTitleArr release];
    [p_ScanRootController release];
    [super dealloc];
}

-(void) PopNavgation{
    [self popViewControllerAnimated:YES];
}

#pragma -
#pragma UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [p_CellRowTitleArr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;{
    static NSString* tIdCell =@"qrBuildCell";
    UITableViewCell* tCell = [tableView dequeueReusableCellWithIdentifier:tIdCell];
    NSInteger row = [indexPath row];
    if (tCell == nil) {
        tCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tIdCell] autorelease];
        UIImageView* tTitleImg = [[UIImageView alloc] initWithFrame:CGRectMake(5, 4, 44, 52)];
        [tCell.contentView addSubview:tTitleImg];
        tTitleImg.tag =InCellImgTag;
        [tTitleImg release];
        UILabel* tTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(64, 4, 200, 52)];
        tTextLabel.tag = InCellTextTag;
        [tCell.contentView addSubview:tTextLabel];
        [tTextLabel release];
    }
    UIImageView* tTitleImg = (UIImageView*)[tCell.contentView viewWithTag:InCellImgTag];
    [tTitleImg setImage:[p_CellImageTitleArr objectAtIndex:row]];
    
    UILabel* tTextLabel = (UILabel*)[tCell.contentView viewWithTag:InCellTextTag];
    [tTextLabel setText:[p_CellRowTitleArr objectAtIndex:row]];
    
    return tCell;
}
#pragma -
#pragma UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger row = [indexPath row];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (row+100 == Type_SMS) {
        UIAlertView* tAlert =[[UIAlertView alloc] initWithTitle:@"提示" message:@"短信未确定" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil];
        [tAlert show];
        [tAlert release];
        return;
    }
    InputContentController* tNextVC = [[InputContentController alloc] init];
    tNextVC.m_ShowType = row+100;
    [self pushViewController:tNextVC animated:YES];
    [tNextVC release];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 58;
}

#pragma -
#pragma UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if (![viewController isEqual:p_ScanRootController]) {
        if ([p_PopNavButton superview] ==nil) {
            [navigationController.navigationBar addSubview:p_PopNavButton];
        }
        if ([viewController isKindOfClass:[InputContentController class]]) {
            InputContentController* tInput =(InputContentController*)viewController;
            [tInput.m_CreateQR removeFromSuperview];
            if (tInput.m_ShowType != Type_Mecard) {
                [navigationController.navigationBar addSubview:tInput.m_CreateQR];
            }
        }else if([viewController isKindOfClass:[ShowQRViewController class]]){
            InputContentController* tInput = [navigationController.viewControllers objectAtIndex:[navigationController.viewControllers count]-2];
            [tInput.m_CreateQR removeFromSuperview];
            ShowQRViewController* tShowQR =(ShowQRViewController*) viewController;
            [self.navigationBar addSubview:tShowQR.m_SaveQRImgButton];
        }
    }else{
        [p_PopNavButton removeFromSuperview];
    }
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if ([viewController isEqual:p_ScanRootController]) {
        UILabel* tNavTitle = (UILabel*)[navigationController.navigationBar viewWithTag:NavBarTitleTag];
        tNavTitle.text =@"创建";
    }else if([viewController isKindOfClass:[InputContentController class]]){
        InputContentController* tInput =(InputContentController*)viewController;
        UILabel* tNavTitle = (UILabel*)[navigationController.navigationBar viewWithTag:NavBarTitleTag];
        tNavTitle.text =[p_NavbarTitleArr objectAtIndex:tInput.m_ShowType -100];
    }else if([viewController isKindOfClass:[ShowQRViewController class]]){
        UILabel* tNavTitle = (UILabel*)[navigationController.navigationBar viewWithTag:NavBarTitleTag];
        tNavTitle.text =@"QR图";
    }
}
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item{
    return NO;
}
@end
